package com.fintech.third.service.bairong;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.common.enums.RespStatus;
import com.fintech.common.service.BaseServiceImpl;
import com.fintech.common.util.ApiResponse;
import com.fintech.third.constant.ThirdMethodConstants;
import com.fintech.third.dao.interfacedata.EdcDataCallOperationRecordsMapper;
import com.fintech.third.dao.interfacedata.EdcProductInterfaceEffecttimeConfigMapper;
import com.fintech.third.model.EquipmentInfor;
import com.fintech.third.model.bairong.CurrentEquipmentInformationReport;
import com.fintech.third.model.bairong.CurrentEquipmentPackageInfo;
import com.fintech.third.model.bairong.EquipmentInfo;
import com.fintech.third.model.bairong.EquipmentSpecialList;
import com.fintech.third.model.bairong.IncomeMonthLevel;
import com.fintech.third.model.bairong.LoanFacilityInformation;
import com.fintech.third.model.bairong.LoanFacilityPackageInfo;
import com.fintech.third.model.bairong.LoginFacilityInformation;
import com.fintech.third.model.bairong.LoginFacilityPackageInfo;
import com.fintech.third.model.bairong.OccApplyLoanInfo;
import com.fintech.third.model.bairong.OccRuleInformation;
import com.fintech.third.model.bairong.OccScoreInfo;
import com.fintech.third.model.bairong.PaymentConsum;
import com.fintech.third.model.bairong.RegisterFacilityInformation;
import com.fintech.third.model.bairong.RegisterFacilityPackageInfo;
import com.fintech.third.model.bairong.SpecialList;
import com.fintech.third.model.bairong.Stability;
import com.fintech.third.model.interfacedata.EdcDataCallOperationRecordsDto;
import com.fintech.third.model.interfacedata.EdcProductInterfaceEffecttimeConfigDto;
import com.fintech.third.utils.ThirdSystemUtil;

import net.sf.json.JSONObject;

@Service("freshOneCreditCard")
public class FreshOneCreditCardImpl extends BaseServiceImpl implements OneCreditCardService {
	
	@Autowired
	private EdcProductInterfaceEffecttimeConfigMapper edcProductInterfaceEffecttimeConfigMapper;
	@Autowired(required = true)
	private OneCreditCardService OneCreditCard;
	@Autowired
	private EdcDataCallOperationRecordsMapper edcDataCallOperationRecordsMapper;

	@Override
	public ApiResponse<CurrentEquipmentInformationReport> cashFacilityInformationReport(JSONObject arg0, String arg1,
			String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<CurrentEquipmentPackageInfo> cashFacilityInformationReportPack(Map<String, String> hashMap) {
		String productType = hashMap.get("productType");// 产品id
		String interfaceCode = ThirdMethodConstants.YK0009;// 接口小类id
		String custIdCard = hashMap.get("custIdCard");
		String mobile = hashMap.get("mobile");
		String loanId = hashMap.get("loanId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("productType", productType);
		map.put("interfaceCode", interfaceCode);
		ApiResponse apiResponse = new ApiResponse();
		// 先查询接口的有效期设置 如果未设置则走以前老的接口 如果设置了则判断数据存在与否及数据是否过有效期
		EdcProductInterfaceEffecttimeConfigDto edcProductInterfaceEffecttimeConfigDto = edcProductInterfaceEffecttimeConfigMapper
				.selectEdcProInterfaceTimeConfigSig(map);
		if (edcProductInterfaceEffecttimeConfigDto == null
				|| (edcProductInterfaceEffecttimeConfigDto != null && edcProductInterfaceEffecttimeConfigDto.getIsUser()
						&& edcProductInterfaceEffecttimeConfigDto.getValidityDate().equals("0"))) {
			return OneCreditCard.cashFacilityInformationReportPack(hashMap);
		} else if (edcProductInterfaceEffecttimeConfigDto != null
				&& !edcProductInterfaceEffecttimeConfigDto.getIsUser()) {
			apiResponse.setCode(RespStatus.SUCCESS.getCode());
			apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			return apiResponse;
		} else {
			// 判断数据是否存在
			Map<String, Object> conditionMap = new HashMap<String, Object>();
			conditionMap.put("interfaceCode", interfaceCode);
			conditionMap.put("custIdCard", custIdCard);
			conditionMap.put("mobile", mobile);
			conditionMap.put("validityEndDate", edcProductInterfaceEffecttimeConfigDto.getValidityDate());
			EdcDataCallOperationRecordsDto edcDataCallOperationRecordsDto = edcDataCallOperationRecordsMapper
					.selectEdcDataCallOperationRecordSig(conditionMap);
			if (edcDataCallOperationRecordsDto == null) {// 数据不存在 (为第一次调用
															// 或数据已过有效期)
				// 调用以前接口
				apiResponse = OneCreditCard.cashFacilityInformationReportPack(hashMap);

				// 插入第三方数据调用记录
				if (apiResponse.getCode() == RespStatus.SUCCESS.getCode()
						|| apiResponse.getCode() == RespStatus.DATABASE_ERROR.getCode()) {
					EdcDataCallOperationRecordsDto edcDataCallOperationRecords = new EdcDataCallOperationRecordsDto();
					edcDataCallOperationRecords.setCustIdCard(custIdCard);
					edcDataCallOperationRecords.setInterfaceCode(interfaceCode);
					edcDataCallOperationRecords.setLoanId(loanId);
					edcDataCallOperationRecords.setMobile(mobile);
					edcDataCallOperationRecords.setProductType(productType);
					edcDataCallOperationRecords.setValidityEndDate(ThirdSystemUtil.getAddDay(new Date(),
							Integer.parseInt(edcProductInterfaceEffecttimeConfigDto.getValidityDate())));
					saveEdcDataCallOperationRecords(edcDataCallOperationRecords);
				}

			} else {
				apiResponse.setCode(RespStatus.SUCCESS.getCode());
				apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			}
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<EquipmentInfo> equipmentInfo(Map<String, String> arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<EquipmentInfor> equipmentInformationVerification(Map<String, String> arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<EquipmentSpecialList> equipmentSpecialListInfo(Map<String, String> arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<LoanFacilityInformation> loanFacilityInformationReport(JSONObject arg0, String arg1, String arg2,
			String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<LoanFacilityPackageInfo> loanFacilityInformationReportPack(Map<String, String> hashMap) {

		String loanId = hashMap.get("loanId");
		String interfaceCode = ThirdMethodConstants.YK0007;
		String productType = hashMap.get("productType");// 产品id
		String mobile = hashMap.get("mobile");
		String custIdCard = hashMap.get("custIdCard");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("productType", productType);
		map.put("interfaceCode", interfaceCode);
		ApiResponse apiResponse = new ApiResponse();
		// 先查询接口的有效期设置 如果未设置则走以前老的接口 如果设置了则判断数据存在与否及数据是否过有效期
		EdcProductInterfaceEffecttimeConfigDto edcProductInterfaceEffecttimeConfigDto = edcProductInterfaceEffecttimeConfigMapper
				.selectEdcProInterfaceTimeConfigSig(map);
		if (edcProductInterfaceEffecttimeConfigDto == null
				|| (edcProductInterfaceEffecttimeConfigDto != null && edcProductInterfaceEffecttimeConfigDto.getIsUser()
						&& edcProductInterfaceEffecttimeConfigDto.getValidityDate().equals("0"))) {
			return OneCreditCard.loanFacilityInformationReportPack(hashMap);
		} else if (edcProductInterfaceEffecttimeConfigDto != null
				&& !edcProductInterfaceEffecttimeConfigDto.getIsUser()) {
			apiResponse.setCode(RespStatus.SUCCESS.getCode());
			apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			return apiResponse;
		} else {
			// 判断数据是否存在
			Map<String, Object> conditionMap = new HashMap<String, Object>();
			conditionMap.put("interfaceCode", interfaceCode);
			conditionMap.put("custIdCard", custIdCard);
			conditionMap.put("mobile", mobile);
			conditionMap.put("validityEndDate", edcProductInterfaceEffecttimeConfigDto.getValidityDate());
			EdcDataCallOperationRecordsDto edcDataCallOperationRecordsDto = edcDataCallOperationRecordsMapper
					.selectEdcDataCallOperationRecordSig(conditionMap);
			if (edcDataCallOperationRecordsDto == null) {// 数据不存在 (为第一次调用
															// 或数据已过有效期)
				// 调用以前接口
				apiResponse = OneCreditCard.loanFacilityInformationReportPack(hashMap);

				// 插入第三方数据调用记录
				if (apiResponse.getCode() == RespStatus.SUCCESS.getCode()
						|| apiResponse.getCode() == RespStatus.DATABASE_ERROR.getCode()) {
					EdcDataCallOperationRecordsDto edcDataCallOperationRecords = new EdcDataCallOperationRecordsDto();
					edcDataCallOperationRecords.setCustIdCard(custIdCard);
					edcDataCallOperationRecords.setInterfaceCode(interfaceCode);
					edcDataCallOperationRecords.setLoanId(loanId);
					edcDataCallOperationRecords.setMobile(mobile);
					edcDataCallOperationRecords.setProductType(productType);
					edcDataCallOperationRecords.setValidityEndDate(ThirdSystemUtil.getAddDay(new Date(),
							Integer.parseInt(edcProductInterfaceEffecttimeConfigDto.getValidityDate())));
					saveEdcDataCallOperationRecords(edcDataCallOperationRecords);
				}

			} else {
				apiResponse.setCode(RespStatus.SUCCESS.getCode());
				apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			}
		}
		return apiResponse;
	
	}

	@Override
	public ApiResponse<LoginFacilityInformation> loginFacilityInformationReport(JSONObject arg0, String arg1,
			String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<LoginFacilityPackageInfo> loginFacilityInformationReportPack(Map<String, String> hashMap) {
		String productType = hashMap.get("productType");// 产品id
		String interfaceCode = ThirdMethodConstants.YK0008;
		String custIdCard = hashMap.get("custIdCard");
		String mobile = hashMap.get("mobile");
		String loanId = hashMap.get("loanId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("productType", productType);
		map.put("interfaceCode", interfaceCode);
		ApiResponse apiResponse = new ApiResponse();
		// 先查询接口的有效期设置 如果未设置则走以前老的接口 如果设置了则判断数据存在与否及数据是否过有效期
		EdcProductInterfaceEffecttimeConfigDto edcProductInterfaceEffecttimeConfigDto = edcProductInterfaceEffecttimeConfigMapper
				.selectEdcProInterfaceTimeConfigSig(map);
		if (edcProductInterfaceEffecttimeConfigDto == null
				|| (edcProductInterfaceEffecttimeConfigDto != null && edcProductInterfaceEffecttimeConfigDto.getIsUser()
						&& edcProductInterfaceEffecttimeConfigDto.getValidityDate().equals("0"))) {
			return OneCreditCard.loginFacilityInformationReportPack(hashMap);
		} else if (edcProductInterfaceEffecttimeConfigDto != null
				&& !edcProductInterfaceEffecttimeConfigDto.getIsUser()) {
			apiResponse.setCode(RespStatus.SUCCESS.getCode());
			apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			return apiResponse;
		} else {
			// 判断数据是否存在
			Map<String, Object> conditionMap = new HashMap<String, Object>();
			conditionMap.put("interfaceCode", interfaceCode);
			conditionMap.put("custIdCard", custIdCard);
			conditionMap.put("mobile", mobile);
			conditionMap.put("validityEndDate", edcProductInterfaceEffecttimeConfigDto.getValidityDate());
			EdcDataCallOperationRecordsDto edcDataCallOperationRecordsDto = edcDataCallOperationRecordsMapper
					.selectEdcDataCallOperationRecordSig(conditionMap);
			if (edcDataCallOperationRecordsDto == null) {// 数据不存在 (为第一次调用
															// 或数据已过有效期)
				// 调用以前接口
				apiResponse = OneCreditCard.loginFacilityInformationReportPack(hashMap);

				// 插入第三方数据调用记录
				if (apiResponse.getCode() == RespStatus.SUCCESS.getCode()
						|| apiResponse.getCode() == RespStatus.DATABASE_ERROR.getCode()) {
					EdcDataCallOperationRecordsDto edcDataCallOperationRecords = new EdcDataCallOperationRecordsDto();
					edcDataCallOperationRecords.setCustIdCard(custIdCard);
					edcDataCallOperationRecords.setInterfaceCode(interfaceCode);
					edcDataCallOperationRecords.setLoanId(loanId);
					edcDataCallOperationRecords.setMobile(mobile);
					edcDataCallOperationRecords.setProductType(productType);
					edcDataCallOperationRecords.setValidityEndDate(ThirdSystemUtil.getAddDay(new Date(),
							Integer.parseInt(edcProductInterfaceEffecttimeConfigDto.getValidityDate())));
					saveEdcDataCallOperationRecords(edcDataCallOperationRecords);
				}

			} else {
				apiResponse.setCode(RespStatus.SUCCESS.getCode());
				apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			}
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<IncomeMonthLevel> occAccountchangeMonthReport(JSONObject arg0, String arg1, String arg2,
			String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<OccApplyLoanInfo> occApplyLoan(JSONObject arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<PaymentConsum> occPaymentConsumReport(JSONObject arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<OccRuleInformation> occRuleReport(JSONObject arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<OccScoreInfo> occScoreReport(JSONObject arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<SpecialList> occSpecialListReport(JSONObject arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<Stability> occTabilityReport(JSONObject arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse packAllInterfaceInfo(JSONObject arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<RegisterFacilityInformation> registerFacilityInformationReport(JSONObject arg0, String arg1,
			String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ApiResponse<RegisterFacilityPackageInfo> registerFacilityInformationReportPack(Map<String, String> hashMap) {
		String productType = hashMap.get("productType");// 产品id
		String interfaceCode = ThirdMethodConstants.YK0006;
		String custIdCard = hashMap.get("custIdCard");
		String mobile = hashMap.get("mobile");
		String loanId = hashMap.get("loanId");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("productType", productType);
		map.put("interfaceCode", interfaceCode);
		ApiResponse apiResponse = new ApiResponse();
		// 先查询接口的有效期设置 如果未设置则走以前老的接口 如果设置了则判断数据存在与否及数据是否过有效期
		EdcProductInterfaceEffecttimeConfigDto edcProductInterfaceEffecttimeConfigDto = edcProductInterfaceEffecttimeConfigMapper
				.selectEdcProInterfaceTimeConfigSig(map);
		if (edcProductInterfaceEffecttimeConfigDto == null
				|| (edcProductInterfaceEffecttimeConfigDto != null && edcProductInterfaceEffecttimeConfigDto.getIsUser()
						&& edcProductInterfaceEffecttimeConfigDto.getValidityDate().equals("0"))) {
			return OneCreditCard.registerFacilityInformationReportPack(hashMap);
		} else if (edcProductInterfaceEffecttimeConfigDto != null
				&& !edcProductInterfaceEffecttimeConfigDto.getIsUser()) {
			apiResponse.setCode(RespStatus.SUCCESS.getCode());
			apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			return apiResponse;
		} else {
			// 判断数据是否存在
			Map<String, Object> conditionMap = new HashMap<String, Object>();
			conditionMap.put("interfaceCode", interfaceCode);
			conditionMap.put("custIdCard", custIdCard);
			conditionMap.put("mobile", mobile);
			conditionMap.put("validityEndDate", edcProductInterfaceEffecttimeConfigDto.getValidityDate());
			EdcDataCallOperationRecordsDto edcDataCallOperationRecordsDto = edcDataCallOperationRecordsMapper
					.selectEdcDataCallOperationRecordSig(conditionMap);
			if (edcDataCallOperationRecordsDto == null) {// 数据不存在 (为第一次调用
															// 或数据已过有效期)
				// 调用以前接口
				apiResponse = OneCreditCard.registerFacilityInformationReportPack(hashMap);

				// 插入第三方数据调用记录
				if (apiResponse.getCode() == RespStatus.SUCCESS.getCode()
						|| apiResponse.getCode() == RespStatus.DATABASE_ERROR.getCode()) {
					EdcDataCallOperationRecordsDto edcDataCallOperationRecords = new EdcDataCallOperationRecordsDto();
					edcDataCallOperationRecords.setCustIdCard(custIdCard);
					edcDataCallOperationRecords.setInterfaceCode(interfaceCode);
					edcDataCallOperationRecords.setLoanId(loanId);
					edcDataCallOperationRecords.setMobile(mobile);
					edcDataCallOperationRecords.setProductType(productType);
					edcDataCallOperationRecords.setValidityEndDate(ThirdSystemUtil.getAddDay(new Date(),
							Integer.parseInt(edcProductInterfaceEffecttimeConfigDto.getValidityDate())));
					saveEdcDataCallOperationRecords(edcDataCallOperationRecords);
				}

			} else {
				apiResponse.setCode(RespStatus.SUCCESS.getCode());
				apiResponse.setMsg(RespStatus.SUCCESS.getMsg());
			}
		}
		return apiResponse;
	}
	
	
}
